package com.dome.base.utils;

import cn.hutool.core.util.StrUtil;
import com.dome.base.constant.CodeConstants;
import com.dome.client.config.exception.PublicException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 断言
 */
public class AssertUtil {
    private static final Logger logger = LoggerFactory.getLogger(AssertUtil.class);
    private static int ASSERT_ERROR_CODE = 90001;

    private static void createExceptionObject(final Integer code,  final String message, Object... params) {
        final PublicException bx =  new PublicException(code, StrUtil.format(message, params));
        throw bx;
    }

    public static void isNull(final Object object) {

        isNull(object, "业务参数必须为空");
    }

    public static void isNull(final Object object, final String message, Object... params) {
        if (object != null) {
            createExceptionObject(ASSERT_ERROR_CODE, message,params);
        }
    }

    public static void isNull(final Object object,final Integer code, final String message, Object... params) {
        if (object != null) {
            createExceptionObject(code, message,params);
        }
    }

    public static void notNull(final Object object,final Integer code, final String message, Object... params) {
        if (object == null) {

            createExceptionObject(code, message,params);
        }
    }

    public static void notNull(final Object object, final String message, Object... params) {
        if (object == null) {

            createExceptionObject(CodeConstants.NO_NULL_EMPTY_PARAM, message,params);
        }
    }

    public static void notNull(final Object object) {

        notNull(object, "业务参数不能为空");
    }

    public static void hasLength(final String text, final String message, Object... params) {
        if (StringUtils.hasLength(text) == false) {

            createExceptionObject(ASSERT_ERROR_CODE, message,params);
        }
    }

    public static void hasLength(final String text) {

        hasLength(text, "字符长度不够，不能为空或者空格");
    }

    public static void hasText(final String text, final String message, Object... params) {
        if (!StringUtils.hasText(text)) {

            createExceptionObject(ASSERT_ERROR_CODE, message,params);
        }
    }

    public static void hasText(final String text) {

        hasText(text, "字符必須要有內容，不能为空或者空格");
    }

    public static void doesNotContain(final String textToSearch, final String substring, final String message, Object... params) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring)
                && textToSearch.contains(substring)) {
            createExceptionObject(ASSERT_ERROR_CODE, message,params);
        }
    }

    public static void doesNotContain(final String textToSearch, final String substring) {

        doesNotContain(textToSearch, substring, "字符串 [" + textToSearch + "] 不能包含 字符串 [" + substring + "]");
    }
    public static void notEmpty(String text,int code, String message, Object... params) throws IllegalArgumentException {
        if (StrUtil.isEmpty(text)) {
            createExceptionObject(code, message,params);
        }
    }

    public static void notEmpty(String text, String message, Object... params) throws IllegalArgumentException {
        if (StrUtil.isEmpty(text)) {
            createExceptionObject(CodeConstants.NO_NULL_EMPTY_PARAM, message,params);
        }
    }
    public static void notEmpty(String text) throws IllegalArgumentException {
        if (StrUtil.isEmpty(text)) {
            createExceptionObject(CodeConstants.NO_NULL_EMPTY_PARAM,"字符必須要有內容");
        }
    }
    public static void notEmpty(final Object[] array, final String message, Object... params) {
        if (org.springframework.util.ObjectUtils.isEmpty(array)) {
            createExceptionObject(CodeConstants.NO_NULL_EMPTY_PARAM, message,params);
        }
    }

    public static void notEmpty(final Object[] array) {

        notEmpty(array, "数据数组不能为空: 必须至少包含一条数据");
    }

    public static void noNullElements(final Object[] array, final String message, Object... params) {
        if (array != null) {
            for (final Object element : array) {
                if (element == null) {
                    createExceptionObject(ASSERT_ERROR_CODE, message,params);
                }
            }
        }
    }

    public static void noNullElements(final Object[] array) {

        noNullElements(array, "数组不能包括任何空的数据，不能有一条空的数据");
    }

    public static void notEmpty(final Collection<?> collection, final String message, Object... params) {
        if (CollectionUtils.isEmpty(collection)) {

            createExceptionObject(ASSERT_ERROR_CODE, message,params);
        }
    }

    public static void notEmpty(final Collection<?> collection) {

        notEmpty(collection, "数据列表不能为空，至少包括一条数据");
    }

    public static void notEmpty(final Map<?, ?> map, final String message, Object... params) {
        if (CollectionUtils.isEmpty(map)) {
            createExceptionObject(ASSERT_ERROR_CODE, message,params);
        }
    }

    public static void notEmpty(final Map<?, ?> map) {

        notEmpty(map, "数据映射集合不能为空，至少包括条数据");
    }

    public static void isInstanceOf(final Class<?> clazz, final Object obj) {

        isInstanceOf(clazz, obj, "");
    }

    public static void isInstanceOf(final Class<?> type, final Object obj, final String message, Object... params) {
        notNull(type, "检查JAVA对象类型为空");
        if (!type.isInstance(obj)) {
            throw new IllegalArgumentException((StringUtils.hasLength(message) ? message + " " : "") + "对象的类 ["
                    + (obj != null ? obj.getClass().getName() : "空") + "] 必须是 【" + type + "】的一个实例");
        }
    }

    public static void isAssignable(final Class<?> superType, final Class<?> subType) {
        isAssignable(superType, subType, "");
    }

    public static void isAssignable(final Class<?> superType, final Class<?> subType, final String message, Object... params) {
        notNull(superType, "检查JAVA对象类型为空");
        if (subType == null || !superType.isAssignableFrom(subType)) {
            throw new IllegalArgumentException(
                    (StringUtils.hasLength(message) ? message + " " : "") + subType + " 不是继承类  " + superType);
        }
    }

}